
#include "UtilManagerDll.h"
#include "Runtime.Low/Services.h"

using namespace std;
using namespace ttg::services;
using namespace ttg_internal::utils;

//----------------------
//--- UtilManagerDll ---
//----------------------

UtilManagerDll *UtilManagerDll::ref = NULL;

std::vector<std::string> UtilManagerDll::getDllFiles()
{
	vector<string> files;
	//Adding path from current directory.
	files.push_back("UtilManager.dll");
	//Trying to get path from TTGLIB_HOME.
	char buf[1024];
	if (GetEnvironmentVariableA("TTGLIB_HOME", buf, 1024) != 0)
	{
		files.push_back(string(buf) + "/Bin/UtilManager.dll");
	}
	return files;
}

UtilManagerDll::UtilManagerDll()
{
	//Nulling references:
	//Initialization.
	init = NULL;
	activateWindow = NULL;
	close = NULL;
	//Parameters.
	createParameterSet = NULL;
	createRootParameterSet = NULL;
	releaseParameterSet = NULL;
	createParameter = NULL;
	createRootParameter = NULL;
	releaseParameter = NULL;
	getParameterValue = NULL;
	releaseString = NULL;
	setParameterValue = NULL;
	setParameterReadOnly = NULL;
	lockParameter = NULL;
	unLockParameter = NULL;
	wasParameterModified = NULL;
	//Pipelines.
	createPipeline = NULL;
	releasePipeline = NULL;
	//Utilities.
	initUtility = NULL;
	updateUtility_Devices = NULL;
	closeUtility = NULL;
}

bool UtilManagerDll::bind()
{
	vector<string> files = getDllFiles();
	//Loading.
	for (size_t i = 0; i < files.size(); i++)
	{
		try
		{
			//Initialization.
			DllLoader::getRef()->load(files[i].c_str(), "init", (FARPROC &)init);
			DllLoader::getRef()->load(files[i].c_str(), "activateWindow", (FARPROC &)activateWindow);
			DllLoader::getRef()->load(files[i].c_str(), "close", (FARPROC &)close);
			//Parameter managment.
			DllLoader::getRef()->load(files[i].c_str(), "createParameterSet", (FARPROC &)createParameterSet);
			DllLoader::getRef()->load(files[i].c_str(), "createRootParameterSet", (FARPROC &)createRootParameterSet);
			DllLoader::getRef()->load(files[i].c_str(), "releaseParameterSet", (FARPROC &)releaseParameterSet);
			DllLoader::getRef()->load(files[i].c_str(), "createParameter", (FARPROC &)createParameter);
			DllLoader::getRef()->load(files[i].c_str(), "createRootParameter", (FARPROC &)createRootParameter);
			DllLoader::getRef()->load(files[i].c_str(), "releaseParameter", (FARPROC &)releaseParameter);
			DllLoader::getRef()->load(files[i].c_str(), "getParameterValue", (FARPROC &)getParameterValue);
			DllLoader::getRef()->load(files[i].c_str(), "releaseString", (FARPROC &)releaseString);
			DllLoader::getRef()->load(files[i].c_str(), "setParameterValue", (FARPROC &)setParameterValue);
			DllLoader::getRef()->load(files[i].c_str(), "setParameterReadOnly", (FARPROC &)setParameterReadOnly);
			DllLoader::getRef()->load(files[i].c_str(), "lockParameter", (FARPROC &)lockParameter);
			DllLoader::getRef()->load(files[i].c_str(), "unLockParameter", (FARPROC &)unLockParameter);
			DllLoader::getRef()->load(files[i].c_str(), "wasParameterModified", (FARPROC &)wasParameterModified);
			//Pipelines.
			DllLoader::getRef()->load(files[i].c_str(), "createPipeline", (FARPROC &)createPipeline);
			DllLoader::getRef()->load(files[i].c_str(), "releasePipeline", (FARPROC &)releasePipeline);
			//Utility managment.
			DllLoader::getRef()->load(files[i].c_str(), "initUtility", (FARPROC &)initUtility);
			DllLoader::getRef()->load(files[i].c_str(), "updateUtility_Devices", (FARPROC &)updateUtility_Devices);
			DllLoader::getRef()->load(files[i].c_str(), "closeUtility", (FARPROC &)closeUtility);
			return true;
		}
		catch (ttg::Error &)
		{ }
	}
	return false;
}

bool UtilManagerDll::unBind()
{
	return true;
}
